<?php
/***
//自我游退改申请结果通知(如同意需要再次发起退款操作)
***/
// include('/var/www/html/new/d/module/api/ZiWoYou/ziwoyou.class.php');
// $zwy = new ZiWoYou();

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$inSide = new SoapClient(null,array(
        "location" => "http://localhost/open/openService/pft_insideMX.php",
        "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);

logfile(json_encode($_REQUEST),'zwy_cancelexamine_data');

$order_id = $_REQUEST['order_id'];
$cancel_num = $_REQUEST['cancel_num'];
$audi_state = $_REQUEST['audi_state'];  

if($order_id){
	$sel = "select * from all_api_order where apiOrder='$order_id' limit 1";
	$GLOBALS['le']->query($sel);
	if($GLOBALS['le']->fetch_assoc()){
		$pftOrder = $GLOBALS['le']->f('pftOrder');  
		$pft_order = $inSide->Order_Globle_Search(
			0,0,0,0,"",
			"","","","","",
			"","",$pftOrder,"",0,
			"","","",1,0,
			0,1,"","","",
			"","","","","",
			"","","1","","");
		$pft_order_xml =simplexml_load_string($pft_order);
		$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
		$UUtprice = (string)$pft_order_xml->Rec->UUtprice;
		if($audi_state == 1){
			$synum = $UUtnum - $cancel_num; //剩余数量		
			// if($synum == 0){
				// $selorder = "update all_api_order set oStatus = 3  where apiOrder='$order_id' limit 1";
				// $GLOBALS['le']->query($selorder);
			// }
			// if($synum > 0){
				// $selorder = "update all_api_order set oStatus = 4,oStnum = $synum where apiOrder='$order_id' limit 1";
				// $GLOBALS['le']->query($selorder);
			// }			
			$money = $synum * $UUtprice;  //剩余总额
			$Cancel = $inSide->Order_Change_Pro($pftOrder,$synum,$money,0,0,"","","");
			if($Cancel == 100){
				$response = <<<xml
				<root>
					<result>true</result>
					<msg>接收成功</msg>
				</root>
xml;
				echo $response;
				logfile($order_id.'|'.$pftOrder.'|'.$Cancel.'|'.$response,'zwy_cancelexamine_s');
				exit;
			}
			else{
				$response = <<<xml
					<root>
						<result>false</result>
						<msg>订单状态不对</msg>
					</root>
xml;
				echo $response;
				logfile($order_id.'|'.$pftOrder.'|'.$Cancel.'|'.$response,'zwy_cancelexamine_f');
				exit;
			}
		}else{
			$response = <<<xml
			<root>
				<result>true</result>
				<msg>接收成功</msg>
			</root>
xml;
			echo $response;
			exit;
		}
		
	}
	else{
		$response = <<<xml
			<root>
				<result>false</result>
				<msg>无此订单</msg>
			</root>
xml;
		echo $response;
		exit;
	}
}else{
	$response = <<<xml
			<root>
				<result>false</result>
				<msg>请输入有效数据</msg>
			</root>
xml;
	echo $response;
	exit;
}

// function logfile($txt, $logname){
	// $fp = fopen('/var/www/html/new/d/logs/api/ZiWoYou/'.$logname.date('Y-m-d').'.txt',"a");
	// flock($fp, LOCK_EX);
	// fwrite($fp, date('Y-m-d H:i:s').'|'.$txt . "\n");
	// flock($fp, LOCK_UN);
	// fclose($fp);
// }

function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/ZiWoYou/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		// $word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/ZiWoYou/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/ZiWoYou/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
?>